package com.fanbuchi.app.ins.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

import com.fanbuchi.app.ins.R;
import com.fanbuchi.app.ins.dialog.LoadingDialog;
import com.fanbuchi.app.ins.event.NetEvent;
import com.fanbuchi.app.ins.event.NetonCompletedEvent;
import com.fanbuchi.app.ins.event.NetonErrorEvent;
import com.fanbuchi.app.ins.event.NetonStartEvent;
import com.fanbuchi.app.ins.vo.enums.AsyncStatus;

import de.greenrobot.event.EventBus;

public class BaseActivity extends AppCompatActivity {
    private   int mCount=0;
    private static final String TAG=BaseActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    public void onEvent(NetonStartEvent event) {
        showDialog();
    }

    public void onEvent(NetonErrorEvent event) {
        //dismiss();
        //event.getException().printStackTrace();
    }

    public void onEvent(NetonCompletedEvent event) {
        dismiss();
    }

    public  void callDial(String phone){
        Intent intentTel = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        startActivity(intentTel);
    }



    public  void visitWeb(String url){
        Uri uri = Uri.parse(url);
        Intent intentWeb = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intentWeb);
    }


    public void startActivity(Class<? extends Activity> intent) {
        Intent _intent= new Intent(this,intent);
        super.startActivity(_intent);
    }

    public  void display(String content){
        if (!isFinishing()){
            Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
        }
    }

    public  void display(int stringsRes){
        if (stringsRes>0){
            display(getResources().getString(stringsRes));
        }
    }

    protected void showDialog() {
        LoadingDialog fragment =(LoadingDialog)getSupportFragmentManager().findFragmentByTag(TAG);
        if (fragment!=null){
            fragment.getDialog().show();
        }else if(fragment==null){
            fragment  = new LoadingDialog();
            Bundle args = new Bundle();
            args.putInt(getString(R.string.app_name), mCount);
            fragment.setArguments(args);
            fragment.show(getSupportFragmentManager(),TAG);
        }
        count(true);
    }



    public void dismiss() {
        dismiss(false);
    }


    public void dismiss(boolean focus) {
        LoadingDialog progressDialogFragment= (LoadingDialog)getSupportFragmentManager()
                .findFragmentByTag(TAG);
        if (progressDialogFragment!=null &&(focus || mCount<=1)){
            progressDialogFragment.dismissAllowingStateLoss();
        }
        count(false);
    }


    private synchronized void count(boolean plus){
        if (plus){
            mCount++;
        }else{
            mCount--;
        }

    }


}
